package com.redtea.studyspringboot.service.impl;

import com.redtea.studyspringboot.domain.entity.TabUser;
import com.redtea.studyspringboot.dao.TabUserDao;
import com.redtea.studyspringboot.service.TabUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import java.util.List;

/**
 * (TabUser)表服务实现类
 *
 * @author makejava
 * @since 2024-04-18 10:43:34
 */
@RequiredArgsConstructor
@Service("tabUserService")
public class TabUserServiceImpl implements TabUserService {
    private final TabUserDao tabUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TabUser queryById(Long id) {
        return this.tabUserDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param tabUser     筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<TabUser> queryByPage(TabUser tabUser, PageRequest pageRequest) {
        long total = tabUserDao.count(tabUser);
        List<TabUser> tabUserList = tabUserDao.queryAllByLimit(tabUser, pageRequest);
        return new PageImpl<>(tabUserList, pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param tabUser 实例对象
     * @return 实例对象
     */
    @Override
    public TabUser insert(TabUser tabUser) {
        this.tabUserDao.insert(tabUser);
        return tabUser;
    }

    /**
     * 修改数据
     *
     * @param tabUser 实例对象
     * @return 实例对象
     */
    @Override
    public TabUser update(TabUser tabUser) {
        this.tabUserDao.update(tabUser);
        return this.queryById(tabUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.tabUserDao.deleteById(id) > 0;
    }
}
